//
//  SearchViewController.m
//  AmountBody
//
//  Created by smm on 2017/12/14.
//  Copyright © 2017年 RCMTM. All rights reserved.
//

#import "SearchViewController.h"
#import "ClassModel.h"
#import "ClassForCollectionViewCell.h"
#import "SearchCollectionReusableView.h"
#import "SearchClassShowView.h"

#define itemWidth (SCREEN_W-60)/5.0
#define itemHeight itemWidth/(100/120.0)+75

@interface SearchViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
@property (strong, nonatomic) UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewLayout* layout;

@property (strong, nonatomic) NSMutableArray * chooseArray;
@property (strong, nonatomic) SearchClassShowView * s_classView;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray * classArr = [NSArray arrayWithObjects:@"衣服",@"男士衬衫",@"男士西装",@"西裤",@"扣子",@"布料",@"风衣",@"毛呢",@"衣服",@"男士衬衫",@"男士西装",@"西裤",@"扣子",@"布料",@"风衣",@"毛呢", nil];
    for (int i=0; i<classArr.count; i++) {
        ClassModel * model = [[ClassModel alloc]init];
        model.name = [classArr objectAtIndex:i];
        model.class_id = [NSString stringWithFormat:@"%d",i];
        [self.chooseArray addObject:model];
    }
    [self loadUI];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [SearchClassShowView blockForSearchButton:^(NSString *searchStr,BOOL isReturn) {
        [_collectionView reloadData];
        [_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
        [_collectionView reloadData];
    }];
    [SearchClassShowView blockForBackButton:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}
#pragma mark - 懒加载

- (NSMutableArray *)dataArray;
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray array]init];
        
    }
    return _dataArray;
}
- (NSMutableArray *)chooseArray;
{
    if (!_chooseArray) {
        _chooseArray = [[NSMutableArray array]init];
        
    }
    return _chooseArray;
}

#pragma mark - 竖屏适配
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    CGFloat searchViewH = 70;
    self.collectionView.frame = CGRectMake(0, searchViewH, SCREEN_W, SCREEN_H-searchViewH);
    [self.collectionView reloadData];
}
#pragma mark -  UI布局
-(void)loadUI
{
    
    CGFloat searchViewH = 70;
//    [_s_classView loadSearchViewWithClassArray:[NSArray array] andHasBack:YES andFirstChoose:0];
    _s_classView = [[SearchClassShowView alloc]init];
    [self.view addSubview:_s_classView];
    [_s_classView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.height.equalTo(@(searchViewH));
    }];
    [_s_classView loadSearchViewWithClassArray:[NSArray array] andHasBack:YES andFirstChoose:@"-1"];
    _s_classView.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    _s_classView.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    _s_classView.layer.shadowOpacity = 0.8;//阴影透明度，默认0
    _s_classView.layer.shadowRadius = 4;//阴影半径，默认3
    UICollectionViewFlowLayout  *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置具体属性
    // 1.设置 最小行间距
    layout.minimumLineSpacing = 10;
    // 2.设置 最小列间距
    layout. minimumInteritemSpacing  = 10;
    // 3.设置item块的大小 (可以用于自适应)
    layout.estimatedItemSize = CGSizeMake(itemWidth, itemHeight);
    // 设置滑动的方向 (默认是竖着滑动的)
    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    // 设置item的内边距
    layout.sectionInset = UIEdgeInsetsMake(1,10,1,10);
    //设置collectionView头视图的大小
    [layout setHeaderReferenceSize:CGSizeMake(_collectionView.frame.size.width,20)];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, searchViewH, SCREEN_W, SCREEN_H-searchViewH) collectionViewLayout:layout];
    _collectionView.backgroundColor = TAB_BAR_COLOR;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ClassForCollectionViewCell class] forCellWithReuseIdentifier:@"ClassForCollectionViewCell2"];
    [self.collectionView registerClass:[SearchCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchCollectionReusableView"];
    [_collectionView reloadData];
    [_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
    [_collectionView reloadData];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --- UICollectionViewDelegate

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(itemWidth, itemHeight);
}

- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    ClassModel *model = [[ClassModel alloc]init];
    
    SearchCollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SearchCollectionReusableView" forIndexPath:indexPath];
    //    headerView.backgroundColor = [UIColor greenColor];
    for (UIView * view in headerView.subviews) {
        [view removeFromSuperview];
    }
    CGFloat i_imageW= SCREEN_W;
    CGFloat i_imageH = SCREEN_W/(2048./200.);
    UILabel * titleLabel = [[UILabel alloc]init];
    titleLabel.text  =@"没有找到符合条件的商品";
    titleLabel.textColor = QianZi_color;
    titleLabel.font  = [UIFont systemFontOfSize:17];
    [titleLabel sizeToFit];
    titleLabel.frame = CGRectMake((SCREEN_W-titleLabel.frame.size.width)/2.0, 20, titleLabel.frame.size.width, Nomal_Label_H);
    
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, titleLabel.frame.origin.y+titleLabel.frame.size.height+20, i_imageW, i_imageH)];
    [image sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:[UIImage imageNamed:@""]];
    image.contentMode = UIViewContentModeScaleAspectFit;
    UIView * nameView = [[UIView alloc]initWithFrame:CGRectMake(0, image.frame.origin.y+image.frame.size.height+20, SCREEN_W, 60)];
    nameView.backgroundColor = RGBColorAlpha(234, 234, 234, 1);
    UILabel * _centerLabel = [[UILabel alloc]init];
    _centerLabel.text = @"为您甄选";
    _centerLabel.font = [UIFont systemFontOfSize:20];
    [_centerLabel sizeToFit];
    CGFloat jianGeH = 5;
    CGFloat imageW = 17;
    CGFloat imageH = 25;
    CGFloat wid = imageW*2+10+_centerLabel.frame.size.width;
    
    UIImageView * _leftImage = [[UIImageView alloc]init];
    _leftImage.frame = CGRectMake((SCREEN_W-wid)/2.0, jianGeH, imageW, imageH);
    _leftImage.image = [UIImage imageNamed:@"@left"];
    _centerLabel.frame = CGRectMake(_leftImage.frame.origin.x+_leftImage.frame.size.width+5, jianGeH, _centerLabel.frame.size.width, 30);
    UIImageView * _rightImage = [[UIImageView alloc]init];
    _rightImage.frame = CGRectMake(_centerLabel.frame.origin.x+_centerLabel.frame.size.width+5, jianGeH, imageW, imageH);
    _rightImage.image = [UIImage imageNamed:@"@right"];
    UILabel * _contentLabel = [[UILabel alloc]init];
    _contentLabel.text = @"Selection for you";
    _contentLabel.textColor = QianZi_color;
    _contentLabel.font = Cell_Big_Font;
    [_contentLabel sizeToFit];
    _contentLabel.frame = CGRectMake((SCREEN_W-_contentLabel.frame.size.width)/2.0, _centerLabel.frame.origin.y+_centerLabel.frame.size.height, _contentLabel.frame.size.width, Nomal_Label_H);
   
    [nameView addSubview:_centerLabel];
    [nameView addSubview:_leftImage];
    [nameView addSubview:_rightImage];
    [nameView addSubview:_contentLabel];
    [headerView addSubview:nameView];
    [headerView addSubview:titleLabel];
    [headerView addSubview:image];
    headerView.frame = CGRectMake(0, 0, SCREEN_W, nameView.frame.origin.y+nameView.frame.size.height);
    return headerView;
}
-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (_dataArray.count <=0) {
        CGSize size = {SCREEN_W, 160+SCREEN_W/(2048./200.)};
        return size;
    }else{
        
        return CGSizeZero;
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.dataArray.count<=0 && self.chooseArray.count>0) {
        return self.chooseArray.count;
    }else{
        return self.dataArray.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    // 需要配置的代码
    //    [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    ClassForCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassForCollectionViewCell2" forIndexPath:indexPath];
    ClassModel * tmpModel = [[ClassModel alloc]init];
    if (_dataArray.count<=0) {
        tmpModel = [_chooseArray objectAtIndex:indexPath.row];
    }else{
        tmpModel = [_dataArray objectAtIndex:indexPath.row];
    }
    
    tmpModel.index = indexPath;
    tmpModel.name = @"测试数据";
    tmpModel.imageUrl = @"nv1";
    cell.backgroundColor = RGBColorAlpha(245, 245, 245, 0.9);
    [cell setCellWithModel:tmpModel];
    return cell;
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
//    [_collectionView reloadData];
//    [_collectionView reloadItemsAtIndexPaths:[_collectionView indexPathsForVisibleItems]];
//    [_collectionView reloadData];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
